import http from '../../apis/api.js';
import dateTimePicker from '../../utils/dateTimePicker.js';
let self;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    detail: null,
    wechatRepairShow:false,/**是否展示报修单处理详情 */
    dealRepair: false,/**是否展示故障单处理 */
    addComponents: false,/**是否展示换上的零件或备件列表 */
    uninstallMaterial: false,/**是否展示换上的零件或备件列表 */
    dateTimeArray1:null,
    startYear: new Date().getFullYear() -2,
    endYear: new Date().getFullYear()+2,
    imageList:[],/**图片列表 */
    submitDisable: false,
    formData:{
      remark: '',
      result: 1,
      startTime: '',
      endTime: '',
    },
    error:{
      result:'',
      startTime:'',
      endTime:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.id = options.id;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.getSendRepairDetail();
    /**查询换上的零件或备件列表*/
    self.getSelfRepairUseList();
    /**查询换下的部件列表*/
    self.getUninstallMaterialList() 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取维修单详情*/ 
  getSendRepairDetail(){
    http.sendRepairDetail(self.data.id).then(res=>{
      self.data.detail = res.content;
      // 获取完整的年月日 时分秒，以及默认显示的数组
      var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, self.data.detail.startTime);
      var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, self.data.detail.endTime );
      self.data.detail.startTimeArray = obj1.dateTime;
      self.data.detail.endTimeArray = obj2.dateTime
      self.data.dealRepair = res.content.state === 0 ? true : false;
      self.setData({
        dateTimeArray1: obj1.dateTimeArray,
        detail: self.data.detail,
        dealRepair: self.data.dealRepair
      })
    })
  },
  /**获取input框内容*/
  getValus(e) {
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'remark':
        self.data.formData.remark = e.detail.value;
        break;
    }
    self.setData({
      formData: self.data.formData
    })
  },
  checked(name) {
    /**表单输入验证 */
    switch (name) {
      case 'startTime':
        if (self.data.formData.startTime === '') {
          self.data.error.startTime = "请输入故障单处理开始时间"
          return false
        }
        self.data.error.startTime = '';
        return true;
        break;
      case 'endTime':
        if (self.data.formData.endTime === '') {
          self.data.error.endTime = "请输入故障单处理结束时间"
          return false
        }
        self.data.error.endTime = '';
        return true;
        break;
      case 'result':
        if (self.data.formData.result === '') {
          self.data.error.result = "请选择处理结果"
          return false
        }
        self.data.error.result = '';
        return true;
        break;
        
    }
  },
  /**切换显示隐藏详情*/ 
  showDetail(e){
    let opt = e.currentTarget.dataset;
    switch (opt.type) {
      case 'wechatRepairShow':
        self.data.wechatRepairShow = !self.data.wechatRepairShow;
        self.setData({
          wechatRepairShow: self.data.wechatRepairShow 
        })
        break;
      case 'dealRepair':
        self.data.dealRepair = !self.data.dealRepair;
        self.setData({
          dealRepair: self.data.dealRepair
        })
        break;
      case 'addComponents':
        self.data.addComponents = !self.data.addComponents;
        self.setData({
          addComponents: self.data.addComponents
        })
        break;
      case "uninstallMaterial":
        self.data.uninstallMaterial = !self.data.uninstallMaterial;
        self.setData({
          uninstallMaterial: self.data.uninstallMaterial
        })
        break;
    }
  },
  /**点击选择处理结果 */
  selectRadio(e) {
    self.data.formData.result = Number(e.currentTarget.dataset.itemvalue);
    self.setData({
      formData: self.data.formData
    })
  },
  /**修改时间 */
  changeDateTime1(e) {
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case "startTime":
        self.data.formData.startTime = `${self.data.dateTimeArray1[0][e.detail.value[0]]}-${self.data.dateTimeArray1[1][e.detail.value[1]]}-${self.data.dateTimeArray1[2][e.detail.value[2]]} ${self.data.dateTimeArray1[3][e.detail.value[3]]}:${self.data.dateTimeArray1[4][e.detail.value[4]]}:${self.data.dateTimeArray1[5][e.detail.value[5]]}`;
        break;
      case "endTime":
        self.data.formData.endTime = `${self.data.dateTimeArray1[0][e.detail.value[0]]}-${self.data.dateTimeArray1[1][e.detail.value[1]]}-${self.data.dateTimeArray1[2][e.detail.value[2]]} ${self.data.dateTimeArray1[3][e.detail.value[3]]}:${self.data.dateTimeArray1[4][e.detail.value[4]]}:${self.data.dateTimeArray1[5][e.detail.value[5]]}`
        break;
    }
    self.setData({ formData: self.data.formData});
   },
  /**列改变时触发*/
  changeDateTimeColumn1(e){
    let type = e.currentTarget.dataset.type;
    var arr, dateArr = this.data.dateTimeArray1;
    switch (type){
      case "startTime":
        arr = self.data.detail.startTimeArray;
        break;
      case "endTime":
        arr = self.data.detail.endTimeArray;
        break;
    }
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray1: dateArr,
      detail: self.data.detail
    });
  },
  submitInfo(){
    self.setData({
      submitDisable: true
    })
    if (self.checked('result') && self.checked('startTime') && self.checked('endTime')) {
      let postData = {
        id:self.data.id,
        startTime: self.data.formData.startTime,
        endTime: self.data.formData.endTime,
        result: self.data.formData.result,
        remark: self.data.formData.remark
      };
      let imageList = this.selectComponent('#imageId').data.imageList; // 页面获取自定义组件实例
      if (imageList.length > 0) {
        imageList.forEach((item, index) => {
          postData['picture' + (index + 1)] = item.path;
        })
      }
      http.updateSendRepair(postData).then(res => {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success() {
            self.getSendRepairDetail();
          },
          complete() {
            self.setData({
              submitDisable: false
            })
          }
        })
      }, error => {
        self.setData({
          submitDisable: false
        })
      })
    } else {
      self.setData({
        error: self.data.error,
        submitDisable: false
      })
    }
  },
  changedComponents(e) {
    /**新增换上的零件或备件列表*/
    let routerUrl = "";
    switch (e.currentTarget.dataset.type) {
      case 'change':
        routerUrl = `/pages/addComponents/addComponents?id=${self.data.id}&repairType=send&pid=${self.data.detail.wechatRepair.plasmaStationId}`
        break
      case 'uninstallMaterial':
        routerUrl = `/pages/addUninstallMaterial/addUninstallMaterial?id=${self.data.id}&repairType=send`
        break
    }
    wx.navigateTo({
      url: routerUrl,
    })

  },
  /**获取换上的零件或备件列表*/
  getSelfRepairUseList() {
    http.sendRepairUseList(self.data.id).then(res => {
      self.data.selfRepairUseList = res.content;
      self.setData({
        selfRepairUseList: self.data.selfRepairUseList
      })
    })
  },
  deleteRepairUse(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'addComponents':
        wx.showModal({
          title: '是否移除换上的零件或备件',
          success(res) {
            if (res.confirm) {
              http.deleteSendRepairUse(id).then(res => {
                wx.showToast({
                  title: '移除成功',
                  icon: 'success',
                  duration: 2000,
                  success() {
                    self.getSelfRepairUseList();
                  }
                })
              })
            }
          }
        })
        break;
      case 'uninstallMaterial':
        wx.showModal({
          title: '是否移除换下的部件',
          success(res) {
            if (res.confirm) {
              http.deleteUninstallMaterial(id).then(res => {
                wx.showToast({
                  title: '移除成功',
                  icon: 'success',
                  duration: 2000,
                  success() {
                    self.getUninstallMaterialList();
                  }
                })
              })
            }
          }
        })
        break;
    }
  },
  /**获取派修单换下的部件*/
  getUninstallMaterialList() {
    http.uninstallMaterialList(self.data.id, 2).then(res => {
      self.data.uninstallMaterialList = res.content;
      console.log(self.data.uninstallMaterialList);
      self.setData({
        uninstallMaterialList: self.data.uninstallMaterialList
      })
    })
  }
})